package ie.dkit.semanticPhotoWeb.client.panels;

import ie.dkit.semanticPhotoWeb.client.Main;
import ie.dkit.semanticPhotoWeb.client.panels.album.AddAlbumWindow;
import ie.dkit.semanticPhotoWeb.client.panels.profile.EditUserProfileWindow;
import ie.dkit.semanticPhotoWeb.client.tools.LoginListener;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarMenuButton;
import com.gwtext.client.widgets.MessageBox.PromptCallback;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class MainToolbar extends Toolbar implements LoginListener {

	private Main m_Main;

	private CenterPanel m_CenterPanel;

	private Item loginItem;

	private Item logoutItem;

	private Item registerItem;

	private Item jumpToUser;

	private Item addAblumItem;

	private Item editUserProfile;

	/**
	 * Creates the MainToolbar for the Application
	 * 
	 * @param main
	 * @param centerPanel
	 */
	public MainToolbar(Main main, CenterPanel centerPanel) {
		m_Main = main;
		m_CenterPanel = centerPanel;
		Menu semanticPhotoWebMenu = new Menu();
		semanticPhotoWebMenu.setShadow(true);
		semanticPhotoWebMenu.setMinWidth(10);

		loginItem = new Item("Login");
		loginItem.addListener(new BaseItemListenerAdapter() {
			public void onClick(BaseItem item, EventObject e) {
				m_Main.gotoLogin();
			}
		});
		logoutItem = new Item("Logout");
		logoutItem.addListener(new BaseItemListenerAdapter() {
			public void onClick(BaseItem item, EventObject e) {
				m_Main.logout();
			}
		});
		logoutItem.setDisabled(true);
		registerItem = new Item("Register / Goto PicasaWeb ");
		registerItem.addListener(new BaseItemListenerAdapter() {
			public void onClick(BaseItem item, EventObject e) {
				m_Main.register();
			}
		});
		semanticPhotoWebMenu.addItem(loginItem);
		semanticPhotoWebMenu.addItem(logoutItem);
		semanticPhotoWebMenu.addItem(registerItem);
		semanticPhotoWebMenu.addSeparator();
		jumpToUser = new Item("Jump to User");
		jumpToUser.addListener(new BaseItemListenerAdapter() {
			public void onClick(BaseItem item, EventObject e) {
				MessageBox
						.prompt(
								"Please type in the username",
								"Please type in the username of the user from which you want to view the album. Usually it is the Google login name",
								new PromptCallback() {
									public void execute(String btnID,
											String text) {
										if (btnID.equals("ok")) {
											m_CenterPanel
													.addUserOverviewPanel(text);

										}
									}
								});
			}
		});
		semanticPhotoWebMenu.addItem(jumpToUser);
		semanticPhotoWebMenu.addSeparator();

		addAblumItem = new Item("Add Album");
		addAblumItem.addListener(new BaseItemListenerAdapter() {
			public void onClick(BaseItem item, EventObject e) {
				new AddAlbumWindow(m_Main.getClientServerConnectorAsync(),
						m_CenterPanel).show(MainToolbar.this.getId());
			}
		});
		addAblumItem.setDisabled(true);
		semanticPhotoWebMenu.addItem(addAblumItem);
		semanticPhotoWebMenu.addSeparator();

		editUserProfile = new Item("Edit User Profile");
		editUserProfile.addListener(new BaseItemListenerAdapter() {
			public void onClick(BaseItem item, EventObject e) {
				new EditUserProfileWindow(m_Main
						.getClientServerConnectorAsync()).show(MainToolbar.this
						.getId());
			}
		});
		editUserProfile.setDisabled(true);
		semanticPhotoWebMenu.addItem(editUserProfile);

		ToolbarMenuButton semanticPhotoWebMenuButton = new ToolbarMenuButton(
				"Semantic Photo Web");
		semanticPhotoWebMenuButton.setMenu(semanticPhotoWebMenu);
		semanticPhotoWebMenuButton.setArrowTooltip("Arrow Tooltip");
		addButton(semanticPhotoWebMenuButton);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.LoginListener#loggedIn(java.lang.String)
	 */
	public void loggedIn(String username) {
		loginItem.disable();
		logoutItem.enable();
		registerItem.disable();
		addAblumItem.enable();
		editUserProfile.enable();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.LoginListener#loggedOut()
	 */
	public void loggedOut() {
		loginItem.enable();
		logoutItem.disable();
		registerItem.enable();
		addAblumItem.disable();
		editUserProfile.disable();
	}
}
